package com.joe.gulishop.product.service.impl;

import com.joe.gulishop.product.dao.AttrAttrgroupRelationDao;
import com.joe.gulishop.product.entity.AttrAttrgroupRelationEntity;
import com.joe.gulishop.product.entity.AttrEntity;
import com.joe.gulishop.product.entity.BrandEntity;
import com.joe.gulishop.product.service.AttrService;
import com.joe.gulishop.product.vo.AttrGroupRelationVo;
import com.joe.gulishop.product.vo.AttrGroupWithAttrVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joe.common.utils.PageUtils;
import com.joe.common.utils.Query;

import com.joe.gulishop.product.dao.AttrGroupDao;
import com.joe.gulishop.product.entity.AttrGroupEntity;
import com.joe.gulishop.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrService attrService;
    //分页查询+模糊查询
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cateLogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
//        如果进行查询的key你是null
        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.like("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if(cateLogId==0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        }else {
            // select * from pms_attr_group where catelog_id = #{cateLogId}
            //and (attr_group_name like concat key or descript like concat key)

            wrapper.eq("catelog_id",cateLogId);

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

    }

    @Override
    public void removeRelation(AttrGroupRelationVo[] relationVos) {
//        relationDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>()
//                .eq("attr_id",relationVos.)
//        )

        relationDao.deleteBatchRelation(relationVos);
    }

    @Override
    public List<AttrGroupWithAttrVo> getCatelogIdWithattr(Long catelogId)
    {
//        查询出所有的品类的属性分组
        List<AttrGroupEntity> attrGroupEntityList = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //根据属性分组查询出所有的文件的属性
        List<AttrGroupWithAttrVo> collect = attrGroupEntityList.stream().map((groupEntity) -> {
            AttrGroupWithAttrVo attrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(groupEntity, attrVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrVo.getAttrGroupId());
            attrVo.setAttrs(relationAttr);
            return attrVo;
        }).collect(Collectors.toList());


        return collect;
    }

}